﻿using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using Product_2.ServiceReference1;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;




namespace Product_2.Views
{
    /// <summary>
    /// Interaction logic for Product_2_View.xaml
    /// </summary>
    public partial class Product_2_View : UserControl
    {
        ServiceReference1.Container context = new ServiceReference1.Container(new Uri("http://localhost:1179/odata"));

        private readonly IUnityContainer container;
        private readonly IRegionManager rManager;

        public Product_2_View(IUnityContainer c, IRegionManager r)
        {
            InitializeComponent();

            container = c;
            rManager = r;

            //DataServiceCollection<Product_1.Views.Product_1_View> context2 = new DataServiceCollection<Product_1.Views.Product_1_View>();

            

            this.productMidListView.ItemsSource = (IEnumerable)context.Product_Mid;

            



        }

        //C#에서 CRUD를 하려면 이렇게 해야한다.
        //1. 먼저 using에 using System.Data.Services.Client; 를 써서 클라이언트의 서비스를 불러온다.
        //2.  DataServiceCollection<table명> lstcontext = new DataServiceCollection<예로 여기선 Product_Mid tabel을 사용>();
        //위와같이 사용할 데이터베이스의 Table명을  DataServiceCollection<>를 사용해서 명명한다.
        // 이렇게하고   context.DeleteObject();  이렇게하고   context.UpdateObject();등을 사용해서 데이터베이트에 업데이트 딜리트시킬수 있다.
        //그리고 자세한 CRUD는 아래의 각 CRUD버튼의 코딩을 보고 이해할것!

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            Uri viewNav = new Uri("Product_1_View", UriKind.Relative);
            rManager.RequestNavigate(RegionNames.MainContentRegion, viewNav);
        }

        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
           Product_Mid product_Mid = context.Product_Mid.Where(kkk => kkk.PM_Id == Int32.Parse(this.PI_txtbox.Text)).SingleOrDefault();

            product_Mid.PM_LotNumber = this.PLN_txtbox.Text;

            if(this.PQ_chkbox.IsChecked == true)
            {
                product_Mid.PM_QC = PQ_chkbox.IsChecked.Value;
            }
            else
            {
                product_Mid.PM_QC = PQ_chkbox.IsChecked.Value;
            }
             
            product_Mid.PM_State = this.PS_txtbox.Text;
            //product_Mid.PM_Date = this.PD_txtbox.Text;


            context.UpdateObject(product_Mid);
            context.SaveChanges();

            DataServiceCollection<Product_Mid> lstContext = new DataServiceCollection<Product_Mid>();
            lstContext.LoadCompleted += (objsender, eventArg) =>
            {
                if (eventArg.Error == null)
                {
                    this.productMidListView.ItemsSource = lstContext;
                }
            };

            lstContext.LoadAsync(context.Product_Mid);
        }

        private void DeleteButton_Click_1(object sender, RoutedEventArgs e)
        {

            var idx = this.productMidListView.SelectedIndex;
            Object product = this.productMidListView.Items.GetItemAt(idx);
            
            
            context.DeleteObject(product);
            context.SaveChanges();

            DataServiceCollection<Product_Mid> lstContext = new DataServiceCollection<Product_Mid>();

            lstContext.LoadCompleted += (objsender, eventArg) =>
            {
                if (eventArg.Error == null)
                {
                    this.productMidListView.ItemsSource = lstContext;
                }
            };

            lstContext.LoadAsync(context.Product_Mid);
        }

        private void RadioButton_Click_1(object sender, RoutedEventArgs e)
        {
            Uri viewNav = new Uri("LoNumberView", UriKind.Relative);
            rManager.RequestNavigate(RegionNames.MainContentRegion, viewNav);
        }
    }
}
